<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>农汇购-新增商pin</title>
<link href="${pageContext.request.contextPath }/css/bootstrap.min.css"
	rel="stylesheet">
<link href="${pageContext.request.contextPath }/layui/css/layui.css"
	rel="stylesheet">
<script src="${pageContext.request.contextPath }/js/jquery.min.js"></script>
<script src="${pageContext.request.contextPath }/js/bootstrap.min.js"></script>
<script src="${pageContext.request.contextPath }/layui/layui.js"></script>
<script type="text/javascript" charset="utf-8" src="${pageContext.request.contextPath }/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="${pageContext.request.contextPath }/ueditor/ueditor.all.min.js"></script>
</head>
<body>
	<form class="layui-form">
		<div class="layui-form-item">
			<label class="layui-form-label" style="width: 88px;">商品名称</label>
			<div class="layui-input-block">
				<input type="text" name="goodsname" required lay-verify="required"
					placeholder="请输入商品名称信息" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label" style="width: 88px;">单价</label>
			<div class="layui-input-block">
				<input type="number" name="price" required lay-verify="required"
					placeholder="请输入商品单价" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label" style="width: 88px;">商品类型</label>
			<div class="layui-input-block">
				<select id="typeid" name="typeid" lay-filter="typeid">

				</select>
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label" style="width: 88px;">批次</label>
			<div class="layui-input-block">
				<input type="number" name="batch" required lay-verify="required"
					placeholder="请输入商品批次" autocomplete="off" class="layui-input">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label" style="width: 88px;">生产日期</label>
			<div class="layui-input-block">
				<input type="text" name="productdate" id="productdate"
					lay-verify="productdate" placeholder="yyyy-MM-dd"
					autocomplete="off" class="layui-input">
			</div>
		</div>
		<input type="hidden" name=imgurl id="imgurl" value=""/>
		<div class="layui-form-item">
			<label class="layui-form-label"
				style="width: 88px; overflow: hidden;">商品展示</label>
			<div class="layui-input-block">
				<button type="button" class="layui-btn" id="test1">上传图片</button>
				<div class="layui-upload-list">
					<img class="layui-upload-img" id="demo1"
						style="width: 95px; height: 90px;">
					<p id="demoText"></p>
				</div>
				<div style="width: 95px;">
					<div class="layui-progress layui-progress-big"
						lay-showpercent="yes" lay-filter="demo">
						<div class="layui-progress-bar" lay-percent=""></div>
					</div>
				</div>
			</div>
		</div>
		<script id="container" name="gooddetail" type="text/plain">
    	</script>


		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
	</form>

	<script>
		function addParent(){
			$.ajax({
				//请求路径
				url:"/rj2002/sale/getTypes",
				//请求方式 get post ...
				type:"post",
				dataType:"json",
				data:{},
				//请求成功后做的事情
				success:function(data){
					data = data.data;
					//添加父级内容
					for(var i=0;i<data.length;i++){
						var option = '<option value="'+data[i].id+'">'+data[i].typename+'</option>';
						$("#typeid").append(option);
					}
					form.render($('#typeid'));
				},
				//请求失败后做的事情
				error:function(XMLHttpRequest,textStatus,errorThrown){
					console.log(XMLHttpRequest)
				}
			});
		}
		$(function(e){
			addParent();
			UE.Editor.prototype._bkGetActionUrl = UE.Editor.prototype.getActionUrl;
			UE.Editor.prototype.getActionUrl = function(action) {
			    if (action == 'uploadimage' || action == 'uploadscrawl' || action == 'uploadimage') {
			        return '/rj2002/ue/saveFile';
			    } else {
			        return this._bkGetActionUrl.call(this, action);
			    }
			}
			ue = UE.getEditor('container', {
				toolbars: [
		        	['fullscreen','source','undo','redo','bold','time','date','simpleupload','edittable','edittd','link','emotion']
		        ],
			    autoHeightEnabled: true,
			    autoFloatEnabled: true
			});
			
		})
	
		var layer,form,ue;
		layui.use([ 'upload', 'element', 'layer', 'form', 'util','laydate' ],function() {
			form = layui.form
			var layer = layui.layer;
			var util = layui.util;
			var laydate = layui.laydate;
			var $ = layui.jquery, upload = layui.upload, element = layui.element;

			//日期
			laydate.render({
				elem : '#productdate'
			});
			var uploadInst = upload.render({
				elem : '#test1',
				url : '/rj2002/sale/uploadFile',
				before : function(obj) {
					//预读本地文件示例，不支持ie8
					obj.preview(function(index, file, result) {
						$('#demo1').attr('src', result); //图片链接（base64）
					});

					element.progress('demo', '0%'); //进度条复位
					layer.msg('上传中', {
						icon : 16,
						time : 0
					});
				},
				done : function(res) {
					if (res.code > 0) {
						return layer.msg('上传失败');
					} else {
						$("#imgurl").val(res.data)
					}
				},
				progress : function(n, elem, e) {
					element.progress('demo', n + '%'); //可配合 layui 进度条元素使用
					if (n == 100) {
						layer.msg('上传完毕', {
							icon : 1
						});
					}
				}
			});

			form.on('submit(formDemo)', function(data){
					console.log(typeof data.field.price)
					console.log(typeof data.field.batch)
				$.ajax({
					//请求路径
					url:"/rj2002/sale/addGoods",
					//请求方式 get post ...
					type:"post",
					dataType:"json",
					data:{
						"batch":data.field.batch,
						"goodsname":data.field.goodsname,
						"imgurl":data.field.imgurl,
						"price":data.field.price,
						"productdate":data.field.productdate,
						"typeid":data.field.typeid,
						"gooddetail":data.field.gooddetail
					},
					//请求成功后做的事情
					success:function(data){
						layer.alert(data.data,{icon: 1},function(index){ window.location.reload(); });
					},
					//请求失败后做的事情
					error:function(XMLHttpRequest,textStatus,errorThrown){
						console.log(XMLHttpRequest)
					}
				});
			    return false;
			 });
		});
	</script>
</body>
</html>